//
//  StaffViewController.m
//  Spa
//
//  Created by PangPond on 10/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "StaffViewController.h"
#import "AddStaffViewController.h"
#import "Employee.h"
@interface StaffViewController ()

@end

@implementation StaffViewController

@synthesize staff = _staff;
@synthesize tableView = _tableView;
@synthesize keyDelete;
@synthesize databaseUtility =_databaseUtility;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadView];
    [self viewDidLoad];
}

- (BOOL) hidesBottomBarWhenPushed {
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
    self.staff = [self.databaseUtility fetchedResultsWithClass:@"Employee"withSortDescriptorString:@"firstname"];
    [self.tableView reloadData];
    UIBarButtonItem * settingButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(AddStaff:)];
    self.navigationItem.rightBarButtonItem = settingButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.staff count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //////
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //config xell
    
    Employee * employees = [self.staff objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@  %@)",employees.nickname,employees.firstname,employees.lastname];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddStaffViewController *addStaff = [[AddStaffViewController alloc] initWithNibName:@"AddStaffViewController" bundle:nil];
    Employee * employees = [self.staff objectAtIndex:indexPath.row];
    addStaff.keyStaffFirstname = employees.firstname;
    addStaff.keyIf = @"1";
    [self presentViewController:addStaff animated:YES completion:nil];
}


//funtion



-(IBAction)AddStaff:(id)sender{
    AddStaffViewController *addStaff = [[AddStaffViewController alloc] initWithNibName:@"AddStaffViewController" bundle:nil];
    addStaff.keyIf = @"2";
    [self presentViewController:addStaff animated:YES completion:nil];
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Employee * employeeKey = [self.staff objectAtIndex:indexPath.row];
    keyDelete = employeeKey.firstname;
    
    NSArray *staffs = [self.databaseUtility fetchedResultsWithClass:@"Employee" withSortDescriptorString:@"firstname" withPredicateString:[NSString stringWithFormat:@"firstname == '%@'",keyDelete]];
    
    Employee *employees = [staffs lastObject];
    
    [self.databaseUtility.managedObjectContext deleteObject:employees];
    [self.databaseUtility saveContext];
    [self viewDidLoad];
}

@end
